package com.t2t.top.devops.controller;

import com.t2t.top.base.controller.BaseController;
import com.t2t.top.base.exception.BizException;
import com.t2t.top.base.model.dto.ResponseDto;
import com.t2t.top.devops.constant.DevopsConstants;
import com.t2t.top.devops.constant.OperCodeConstants;
import com.t2t.top.devops.model.dto.MenuDto;
import com.t2t.top.devops.model.po.Menu;
import com.t2t.top.devops.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yangpengfei
 */
@Controller
@RequestMapping(value = "/devops/menu")
public class MenuController extends BaseController {

    protected Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;

    @RequestMapping("/query")
    @ResponseBody
    public ResponseDto query(MenuDto dto) throws BizException {
        List<Menu> list = menuService.query(dto);
        return ResponseDto.bulidSuccessResult().setData(list);
    }

    @RequestMapping("/update")
    public String update(Menu dto) throws BizException {
        try {
            menuService.update(dto);
            Menu po = menuService.findById(dto.getId());
            if (po.getPid() == -1)
                return DevopsConstants.REDIRECT_URL + "menu-list";
            return DevopsConstants.REDIRECT_URL + "menu-sublist?pid=" + po.getPid();
        } catch (BizException e) {
            ResponseDto responseDto = ResponseDto.bulidFailResult().setMsg(e.getMessage());
            getRequest().setAttribute("dto", responseDto);
            return "common/error";
        }
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Menu po) throws BizException {
        if (po.getId() != null) po = menuService.findById(po.getId());
        this.getRequest().setAttribute("po", po);
        return "menu/update";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseDto delete(@RequestParam("ids") String ids) throws BizException {
        try {
            menuService.delete(ids);
        } catch (BizException e) {
            return ResponseDto.bulidFailResult().setMsg(e.getMessage());
        }
        return ResponseDto.bulidSuccessResult().setMsg("删除成功");
    }

    @RequestMapping(value = "/orderNum")
    @ResponseBody
    public ResponseDto orderNum(@RequestParam("pid") Integer pid) {
        try {
            Integer num = menuService.orderNum(pid);
            return ResponseDto.bulidSuccessResult().setData(num);
        } catch (Exception e) {
            return ResponseDto.bulidFailResult().setMsg(e.getMessage());
        }
    }

    @RequestMapping("/left")
    public String left(Menu dto) throws BizException {
        try {
            List<MenuDto> result = menuService.listAllCanUseMenus();
            getRequest().setAttribute("dto", result);
            return "iframe/left";
        } catch (BizException e) {
            ResponseDto responseDto = ResponseDto.bulidFailResult().setMsg(e.getMessage());
            getRequest().setAttribute("dto", responseDto);
            return "common/error";
        }
    }

    /**
     * 通用页面跳转
     *
     * 规则：iframe-top --> /jsp/iframe/top.jsp
     *
     * @param url
     */
    @RequestMapping("/{url}")
    public String url(@PathVariable(value = "url") String url) throws BizException {
        url = url.replace("-", "/");
        return url;
    }

}
